﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.ModFlow.Domain.DomainLayer;
using HAUtil;

using GridData=HA.ModFlow.Domain.DomainLayer.GridData;



namespace HA.Data
{

    /// <summary>
    /// 这个类在第二轮设计中，可能用于表示三维网格
    /// </summary>
    [Serializable]
    public class Grid3d:DescEntity
    {
        public Grid3d(GridData data,string id):base(id)
        {
            this._modFlowGrid = data;
 
        }


        public Grid3d(GridData data)
        {
            this._modFlowGrid = data;
        }

        public Grid3d(int nrows, int ncols, int nlayers, double xlength, double ylength, double zlength, double orgX, double orgY, double orgZ, double rotate)
        {
            GridConfig config = new GridConfig(false,nrows, ncols, nlayers, xlength, ylength, zlength, orgX, orgY, orgZ, rotate);

            this._modFlowGrid = new GridData(config);
        }


        GridData _modFlowGrid;

        public GridData ModFlowGrid
        {
            get { return _modFlowGrid; }
          
        }




        [field: NonSerialized]
        public event EventHandler<CollecionChangedEventArgs> DataSetCollectionChanged;


       /// <summary>
       /// 结果单独保存，不合主程序数据保存到一起
       /// 一直不为 null
       /// </summary>
        [NonSerialized ]
        List<DSGrid3dCollecion> _datasetCollecion = new List<DSGrid3dCollecion>();


        /// <summary>
        /// 用于表示该网格下的数据集
        /// </summary>
        public List<DSGrid3dCollecion> DatasetCollecion
        {
            get { return _datasetCollecion; }


            set { this._datasetCollecion = value; }
            
        }


        public void Add(DSGrid3dCollecion gc)
        {

            //这里考虑了，当已经存在对应的选项时的情况，需要先移除旧的选项

            DSGrid3dCollecion gcOld = this.GetDatasetCollection(gc.Key.ToString ());



            if (gcOld == null)
            {
                this._datasetCollecion.Add(gc);

            }
            else
            {

                this.Remove(gcOld);

                this._datasetCollecion.Add(gc);

            }
           

           

            if (DataSetCollectionChanged!=null)
            {
                DataSetCollectionChanged(this,new CollecionChangedEventArgs (gc,CollectionChangedType.ADD));
                
            }
 
        }

        public void Remove(DSGrid3dCollecion gc)
        {

            if (this._datasetCollecion.Contains(gc))
            {

                this._datasetCollecion.Remove(gc);

                if (DataSetCollectionChanged != null)
                {
                    DataSetCollectionChanged(this, new CollecionChangedEventArgs(gc, CollectionChangedType.REMOVE));

                }


                
            }



          

        }

        public DSGrid3dCollecion GetDatasetCollection(string id)
        {
            foreach (DSGrid3dCollecion item in this._datasetCollecion)
            {
                if (id==item.Key.ToString())
                {
                    return item;
                    
                }
                
            }
            

            return null;

        }

        public void Clear()
        {
            if (this._datasetCollecion.Count>0)
            {
                this._datasetCollecion.Clear();

                if (DataSetCollectionChanged != null)
                {
                    DataSetCollectionChanged(this, new CollecionChangedEventArgs(null, CollectionChangedType.RESET));

                }

                
            }






        }


    }
}
